﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class AddTask : CustomForm
    {
        private Form1 _parent;

        public AddTask(Form1 parent)
        {
            InitializeComponent();
            _parent = parent;

            foreach(RenderScheme scheme in _parent.Schemes)
            {
                SchemesComboBox.Items.Add(scheme.SchemeName);
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void AddTaskButton_Click(object sender, EventArgs e)
        {
            RenderTask task = new RenderTask();
            task.TaskName = TaskNameTextBox.Text;
            task.RenderScheme = _parent.Schemes.Find(r =>
                r.SchemeName == SchemesComboBox.SelectedItem.ToString());
            foreach(DataGridViewRow row in SourceFilesDataGridView.Rows)
            {
                RenderTaskItem item = new RenderTaskItem();
                item.SourceName = row.Cells["SourceFileName"].Value.ToString();
                item.Status = RenderTaskItemStatus.Ready;
                task.Items.Add(item);
            }
            task.OutputPath = OutputPathTextBox.Text;
            task.Status = RenderTaskStatus.Ready;

            _parent.HandleAddTask(task);
            Close();
        }

        private void SelectSourceFilesButton_Click(object sender, EventArgs e)
        {
            ShowFileDialog("请选择源文档文件", "所有文件(*doc*;*xls*;*ppt*)|*.doc*;*.xls*;*.ppt*", true, control =>
            {
                foreach(string fileName in control.FileNames)
                {
                    int index = SourceFilesDataGridView.Rows.Add();
                    DataGridViewRow row = SourceFilesDataGridView.Rows[index];
                    row.Cells["SourceFileName"].Value = fileName;
                }
            });
        }

        private void SelectOutputPathButton_Click(object sender, EventArgs e)
        {
            ShowFolderDialog("请选择输出文件夹", control =>
            {
                OutputPathTextBox.Text = control.SelectedPath;
            });
        }

        private void DeleteSourceFileButton_Click(object sender, EventArgs e)
        {
            foreach(DataGridViewRow row in SourceFilesDataGridView.SelectedRows)
            {
                SourceFilesDataGridView.Rows.Remove(row);
            }
        }
    }

}
